package mystruct

import (
	"fmt"
	"net"
    "reflect"
    "strconv"
)


func GetIpaddr() (string, error) {
	conn, err := net.Dial("udp", "gitee.com:80")
	if err != nil{
		return "", err
	}
	localAddr := conn.LocalAddr().(*net.UDPAddr)
    ip := localAddr.IP.String()
    conn.Close()
    return ip, nil
}


func Obj_dir(obj interface{}) {
    _dir := reflect.TypeOf(obj)
    for i := 0; i < _dir.NumMethod(); i++{
        fmt.Println(_dir.Method(i).Name)
    }
}

func Obj_type(obj interface{}) {
	fmt.Println(reflect.TypeOf(obj))
}

func StrToInt64(str string) int64 {
	int, _ := strconv.ParseInt(str, 10, 64)
	return int
}

func StrToBool(str string) bool {
	boolValueTrue, _ := strconv.ParseBool(str)
	return boolValueTrue
}

func FindItemsByName(items []ModelItems, value string) ModelItems {
    for _, item := range items {
        if item.Value == value {
            return item
        }
    }
    return ModelItems{Select:""}
}


func safeGetColumnValue(columns []string, index int) string {
    if index < 0 || index >= len(columns) {
        return "" // 返回默认值或记录错误
    }
    return columns[index]
}

func parseValue(valueType, strValue string) (interface{}, error) {
    switch valueType {
    case "string":
        return strValue, nil
    case "int":
        v, err := strconv.ParseInt(strValue, 10, 64)
        return v, err
    case "float":
        v, err := strconv.ParseFloat(strValue, 64)
        return v, err
    case "bool":
        v, err := strconv.ParseBool(strValue)
        return v, err
    case "prgs":
        v, err := strconv.ParseInt(strValue, 10, 64)
        return v, err
    default:
        return strValue, nil
    }
}

func JoinStrings(strs ...string) string {
    // 1. 计算总长度
    totalLen := 0
    for _, s := range strs {
        totalLen += len(s)
    }
    
    // 2. 预分配足够大的字节切片
    buf := make([]byte, 0, totalLen)
    
    // 3. 逐个追加字符串
    for _, s := range strs {
        buf = append(buf, s...)
    }
    
    // 4. 转换为字符串返回
    return string(buf)
}